<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Util\ComUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use App\Model\UserProfile;
class UsersRatingController extends Controller
{
    use ModelForm;

    /**
     * 用户列表
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('用户等级/评级');
            $content->description('评级');
            $content->body($this->grid());
        });
    }


    /**
     * 声优列表
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserProfile());
        $where = [
            ['role', UserProfile::ROLE_HOST]
        ];
        $grid->model()->where($where)->orderBy('id', 'desc');
        // $grid->user_id('内部ID')->sortable();
        $grid->user_profile_id('声优ID')->sortable();
        $grid->avatar('头像')->display(function ($data) {
            if (!$data) return '';
            return ComUtil::buidlImgaeHtml($data);
        });
        $grid->nick_name('昵称');
        $grid->charm_value('魅力值')->sortable();
        $grid->is_recommend('首页推荐')->display(function ($data) {
            return $data == 1 ? "<span class='text-danger text-bold'>推荐</span>" : '-';
        })->sortable();
        $blackHosts = HostBlacklist::getUids();
        $grid->black('是否黑名单')->display(function ($online) use ($blackHosts) {
            return in_array($this->user_id, $blackHosts) ? '拉黑' : '-';
        });
        $grid->hostInfo()->video_answer_num('视频接听')->display(function ($data) {
            return "{$data}次（{$this->video_answer_time}分钟）";
        })->sortable();
        $grid->hostInfo()->voice_answer_num('语音接听')->display(function ($data) {
            return "{$data}次（{$this->voice_answer_time}分钟）";
        })->sortable();
        $grid->hostInfo()->voice_answer_num('语音次数');
        $grid->hostInfo()->income_total('总收入');
        $grid->hostInfo()->income_video('视频收入');
        $grid->hostInfo()->income_msg('私信收入');
        $grid->hostInfo()->income_voice('语音收入');
        $grid->hostInfo()->income_gift('普通礼物收入');
        $grid->hostInfo()->income_sysgift('名义礼物收入');
        $grid->userItem()->status('状态')->display(function ($data) {
            return User::STATUS[$data] ?? '';
        });
        $grid->host_cate('评级')->editable('select', UserProfile::HOST_CATE);
        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        //禁用新增
        $grid->disableCreation();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('user_profile_id', '用户ID');
                $filter->like('nick_name', '用户昵称');
                $filter->equal('userItem.status', '用户状态')->select(User::STATUS);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('userItem.phone', '手机号');
                $filter->equal('is_recommend', '是否首页推荐')->select([1 => '推荐', 0 => '不推荐']);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->between('created_at', '注册时间')->datetime();
                $filter->equal('host_cate', '女用户评级')->select(UserProfile::HOST_CATE);
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) use ($blackHosts) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
        });
        return $grid;
    }
}



